package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.SystemColor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Insert extends JInternalFrame {
	
	Connection conn;
	DatabaseTest main;
	
	private JLabel lblNewLabel;
	private JTextField code;
	private JLabel lblEa;
	private JTextField ea;
	private JButton btnNewButton;
	private JPanel panel;
	private JLabel status;
	private JLabel lblSerial;
	private JTextField serial;

	/**
	 * Create the frame.
	 */
	public Insert(Connection c, DatabaseTest m) {
		super("insert", true, true, true, false);
		setTitle("입력");
		setBounds(100, 100, 300, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getCode());
		getContentPane().add(getLblEa());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getPanel());
		getContentPane().add(getLblSerial());
		getContentPane().add(getSerial());
		conn = c;
		this.main = m;
		serialPlus();
	}
	
	public void serialPlus(){
		String sql = "SELECT NVL(MAX(SERIAL),0)+1 FROM PTBL";
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				serial.setText(""+rs.getInt("NVL(MAX(SERIAL),0)+1"));
			}
			System.out.println(rs.getInt("NVL(MAX(SERIAL),0)+1"));
		} catch (SQLException e) {			
			e.printStackTrace();
		}
	}
	
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("code");
			lblNewLabel.setBounds(34, 57, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(103, 54, 116, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("ea");
			lblEa.setBounds(34, 88, 57, 15);
		}
		return lblEa;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(103, 85, 116, 21);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("저장");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						String c = code.getText();
						int ee = Integer.parseInt(ea.getText());
						
						String sql = "INSERT INTO PTBL(serial, code, ea) VALUES(ptbl_serial.nextval,?,?)";
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setInt(2, ee);
						
						pstmt.executeUpdate();
						status.setText("데이터가 입력되었습니다.");
						
						pstmt.close();
						
						serialPlus();
//						code.setText("");
						ea.setText("");
						
//						StringBuilder sb = new StringBuilder();
//						sb.append("INSERT INTO PTBL VALUES(?,?)");						
						
					} catch (Exception e2) {
						// TODO: handle exception
					}
					
				}
			});
			btnNewButton.setBounds(103, 139, 97, 23);
		}
		return btnNewButton;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(SystemColor.info);
			panel.setBounds(12, 187, 207, 21);
			panel.add(getStatus());
		}
		return panel;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("데이터를 입력해주세요.");
		}
		return status;
	}
	private JLabel getLblSerial() {
		if (lblSerial == null) {
			lblSerial = new JLabel("serial");
			lblSerial.setBounds(34, 29, 57, 15);
		}
		return lblSerial;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setEditable(false);
			serial.setColumns(10);
			serial.setBounds(103, 26, 116, 21);
		}
		return serial;
	}
}
